package com.lzy.system.organization.controller;

import com.lzy.conf.JSONResult;
import com.lzy.system.organization.entity.SysRes;
import com.lzy.system.organization.entity.SysResRole;
import com.lzy.system.organization.entity.SysRole;
import com.lzy.system.organization.service.ISysResRoleService;
import com.lzy.utils.ValidList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色绑定资源 前端控制器
 * </p>
 *
 * @author lcc
 * @since 2022-08-17
 */
@RestController
@RequestMapping("/organization/sysResRole")
@Api(tags = "角色绑定资源")
public class SysResRoleController {
    private ISysResRoleService iSysResRoleService;

    @Autowired
    public SysResRoleController(ISysResRoleService iSysResRoleService) {
        this.iSysResRoleService = iSysResRoleService;
    }

    @ApiOperation("根据资源id查询角色绑定资源")
    @RequestMapping(value = "queryByResId/{redId}", method = RequestMethod.GET)
    public JSONResult queryByResId(@ApiParam("资源id") @PathVariable(value = "redId", required = true) Long redId) throws Exception {
        List<SysResRole> sysResRoles = iSysResRoleService.queryByResId(redId);
        return JSONResult.ok(sysResRoles);
    }

    @ApiOperation("根据角色id查询角色绑定资源")
    @RequestMapping(value = "queryByRoleId/{roleId}", method = RequestMethod.GET)
    public JSONResult queryByRoleId(@ApiParam("角色id") @PathVariable(value = "roleId", required = true) Long roleId) throws Exception {
        List<SysResRole> sysResRoles = iSysResRoleService.queryByRoleId(roleId);
        return JSONResult.ok(sysResRoles);
    }

    @ApiOperation("资源授权(资源)")
    @RequestMapping(value = "savePowerByResId/{redId}", method = RequestMethod.POST)
    public JSONResult savePowerByResId(
            @ApiParam("资源id") @PathVariable(value = "redId", required = true) Long redId,
            @ApiParam("角色信息") @RequestBody @Validated() ValidList<SysRole> sysRoles
    ) throws Exception {
        iSysResRoleService.savePowerByResId(redId, sysRoles);
        return JSONResult.ok();
    }

    @ApiOperation("资源授权(角色)")
    @RequestMapping(value = "savePowerByRoleId/{roleId}", method = RequestMethod.POST)
    public JSONResult savePowerByRoleId(
            @ApiParam("角色id") @PathVariable(value = "roleId", required = true) Long roleId,
            @ApiParam("资源信息") @RequestBody @Validated() ValidList<SysRes> sysResList
    ) throws Exception {
        iSysResRoleService.savePowerByRoleId(roleId, sysResList);
        return JSONResult.ok();
    }
}
